package com.yaoyong.codegenerator.utils;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

/**
 * 文件读写工具类
 *
 * @author 姚勇
 */
public class FileUtil {

    /**
     * 获取文本文件上的内容
     */
    public static String getContent(File file) throws IOException {
        StringBuffer sb = new StringBuffer();
        try (BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file), "UTF-8"))) {
            String s;
            while ((s = br.readLine()) != null) {
                sb.append(s).append("\r\n");
            }
        }
        return sb.toString();
    }

    /**
     * 设置文件内容
     *
     * @param filePath 文件存储目录
     * @param content  文件内容
     */
    public static void setContent(String filePath, String content) {

        try {

            File f = new File(filePath);

            if (f.exists()) {
                f.delete();
            }
            File pf = f.getParentFile();
            if (!pf.exists()) {
                pf.mkdirs();
            }

            f.createNewFile();


            FileOutputStream fos = new FileOutputStream(f);
            BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(fos, "UTF-8"));

            writer.write(content);
            writer.close();

        } catch (Exception e) {
            e.printStackTrace();

        }
    }

    /**
     * 复制文件
     *
     * @param src
     * @param dest
     * @throws IOException
     */
    public static void copyFile(File src, File dest) throws IOException {
        if (src.exists()) {
            //如果目的地文件夹不存在,要创建
            if (!dest.getParentFile().exists()) {
                dest.getParentFile().mkdirs();
            }

            try (
                    FileInputStream fis = new FileInputStream(src);
                    FileOutputStream fos = new FileOutputStream(dest)
            ) {
                byte[] array = new byte[8192];
                int len;
                while ((len = fis.read(array)) != -1) {
                    fos.write(array, 0, len);
                }
            }
        }
    }

    /**
     * 判断文件是否为模板类文件
     *
     * @param fileName
     * @return
     */
    public static boolean isTemplateFile(String fileName) {
        String extensionName = getExtensionName(fileName);
        switch (extensionName) {
            case "html":
            case "htm":
            case "java":
            case "xml":
            case "jsp":
            case "project":
            case "properties":
            case "yml":
            case "component":
                //自定义模板文件类型
            case "(myself)":
                return true;
            default:
                return false;
        }
    }

    /**
     * 获取文件扩展名
     */
    public static String getExtensionName(String fileName) {
        String extension = "";
        if ((fileName != null) && fileName.contains(".")) {
            int dot = fileName.lastIndexOf('.');
            if ((dot > -1) && (dot < (fileName.length() - 1))) {
                extension = fileName.substring(dot + 1);
            }
        }
        return extension;
    }

    /**
     * 首字母大写
     *
     * @param name
     * @return
     */
    public static String getClassName(String name) {
        if (name.equals(name.toUpperCase())){
            name = name.toLowerCase();
        }
        String s0 = name.substring(0, 1).toUpperCase();
        return s0 + name.substring(1);
    }

    /**
     * 骆驼风格名字
     *
     * @param name
     * @return
     */
    public static String getCamelName(String name) {
        if (name.equals(name.toUpperCase())){
            name = name.toLowerCase();
        }
        String[] s = name.split("_");
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < s.length; i++) {
            if (i != 0){
                sb.append(getClassName(s[i]));
            }else {
                sb.append(s[i]);
            }
        }
        return sb.toString();
    }
}
